import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class RegisterPanel extends JFrame{
	private JTextField tf_NewID;
	private JPasswordField tf_NewPW;
	private JPasswordField tf_ConfirmPW;
	public RegisterPanel() {
		
		final JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblNewpw = new JLabel("NewPW: ");
		lblNewpw.setBounds(24, 77, 124, 15);
		panel.add(lblNewpw);
		
		JLabel lblNewid = new JLabel("NewID: ");
		lblNewid.setBounds(24, 38, 124, 15);
		panel.add(lblNewid);
		
		JLabel lblConfirmpw = new JLabel("ConfirmPW: ");
		lblConfirmpw.setBounds(24, 113, 124, 15);
		panel.add(lblConfirmpw);
		
		tf_NewID = new JTextField();
		tf_NewID.setBounds(160, 32, 126, 21);
		panel.add(tf_NewID);
		tf_NewID.setColumns(10);
		
		tf_NewPW = new JPasswordField();
		tf_NewPW.setBounds(160, 71, 126, 21);
		panel.add(tf_NewPW);
		
		tf_ConfirmPW = new JPasswordField();
		tf_ConfirmPW.setBounds(160, 107, 126, 21);
		panel.add(tf_ConfirmPW);
		
		final JLabel lb_IDCheck = new JLabel("");
		lb_IDCheck.setBounds(298, 38, 113, 15);
		panel.add(lb_IDCheck);
		
		final JLabel lb_PWCheck = new JLabel("");
		lb_PWCheck.setBounds(298, 113, 113, 15);
		panel.add(lb_PWCheck);
		
		
		JButton btnRegister = new JButton("Register");
		btnRegister.setBounds(258, 176, 97, 23);
		panel.add(btnRegister);
		
		final JLabel lbregistersucceed = new JLabel("");
		lbregistersucceed.setBounds(231, 209, 249, 15);
		panel.add(lbregistersucceed);
		
		btnRegister.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				
				String NewID = tf_NewID.getText();
				String NewPW = tf_NewPW.getText();
				String ConfirmPW = tf_ConfirmPW.getText();
				
				try
				{
				ArrayList <String> IDList = new ArrayList<String>(); // ArrayList for IDList.txt
				
				FileReader IDListReader = new FileReader("IDList.txt");
				BufferedReader IDListBufferedReader = new BufferedReader(IDListReader);
				
				String line = null;
				
					while((line=IDListBufferedReader.readLine()) != null)
					{
						StringTokenizer st = new StringTokenizer(line);
						IDList.add(st.nextToken());	
					}
					
					Boolean idexist = false;
					for(int i = 0; i<IDList.size();i++)
					{
						if(NewID.equals(IDList.get(i)))
						{
							lb_IDCheck.setText("ID already Exist");
							idexist = true;
						}
					}
					
					if(idexist.equals(false) && NewPW.equals(ConfirmPW))
					{
						
					IDList.add(NewID);
					
					File myfile = new File(NewID+".txt");
					myfile.createNewFile();
						
					FileWriter IDInfoWriter = new FileWriter(NewID + ".txt");
					IDInfoWriter.write(NewPW);

					FileWriter IDListWriter = new FileWriter("IDList.txt");
					for(int i = 0; i<IDList.size();i++)
					{
						IDListWriter.write(IDList.get(i));
						System.out.println(IDList.get(i));
						IDListWriter.write(" \r\n");
					}
					
					IDListWriter.close();
					IDInfoWriter.close();
					
					lbregistersucceed.setText("Register Complete!");

					}
					else
					{
						lb_PWCheck.setText("PW are different");
					}
					
					IDListReader.close();
					IDListBufferedReader.close();
					IDListBufferedReader.close();
				}
					catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}// Read IDList.txt for check id exist
				
			}
		});
		
		
	}
}